package applet;

//import java.util.*;

import java.util.ArrayList;

import processing.core.PApplet;
import de.fhpotsdam.unfolding.geo.Location;

public class Clock {
	float latitude, longitude;
	float timeZone;
	Location location;
	float diameter;
	ArrayList<Integer> hourlyCount;
	float[] hourlyPercentage;
	float sum;
	float startAngle, stopAngle;
	int[] color;
	float xy[];
	String name;
	
	public Clock(double lat, double lon, float tz, ArrayList<Integer> hc, String n) {
		latitude = (float)lat;
		longitude = (float)lon;
		location = new Location(latitude,longitude);

//		location.x = latitude;
//		location.y = longitude;
		name = n;
		timeZone = tz;
		hourlyCount = hc;
		sum = 0;
		for(int i = 0; i < hourlyCount.size(); i++) {
			sum+=hourlyCount.get(i);
		}
		if(sum<100) {
			diameter = 25;
		} else if(sum <1000) {
			diameter = 35;
		} else if(sum < 10000) {
			diameter = 45;
		} else if(sum < 100000) {
			diameter = 60;
		} else if(sum < 1000000) {
			diameter = 75;
		} else {
			diameter = 90;
		}		
		hourlyPercentage = new float[hourlyCount.size()];
		
		for(int i = 0; i < hourlyCount.size(); i++) {
			hourlyPercentage[i]=(float)hourlyCount.get(i)/(float)sum;
		}
		
//		color = new int[9];
		color = new int[7];
		
//		int colorFrom = GLOBAL.processing.color(255,0,0);
//		int colorMid = GLOBAL.processing.color(0,255,0);	
//		int colorTo = GLOBAL.processing.color(0,0,255);
		
		int colorFrom = GLOBAL.processing.color(32,0,0);
		int colorMid = GLOBAL.processing.color(255,0,0);	
		int colorTo = GLOBAL.processing.color(255,224,224);	
		
		for(int i = 0; i < 4; i++) {
			color[i]= GLOBAL.processing.lerpColor(colorFrom, colorMid, (float)i/3f);
		}
		for(int i = 4; i <7 ; i++) {
			color[i]= GLOBAL.processing.lerpColor(colorMid, colorTo, (float)(i-3)/3f);
		}
	}
	
	public void draw() {
		
		xy = GLOBAL.map.map.getScreenPositionFromLocation(location);
		stopAngle = PApplet.PI/2-timeZone/24*2*PApplet.PI;
		GLOBAL.processing.noStroke();
		for(int i = 0; i<24; i++) {
			startAngle = stopAngle - PApplet.PI/12;
			int colorIndex = (int)(PApplet.log(100.0f*(float)hourlyCount.get(i)/sum)/PApplet.log(2));
			if(colorIndex>=7) {colorIndex = 6;}
			if(colorIndex<0) {colorIndex = 0;}
			GLOBAL.processing.fill(color[colorIndex]);
			GLOBAL.processing.arc(xy[0], xy[1], diameter, diameter, startAngle, stopAngle);
			stopAngle -= PApplet.PI/12;
		}
		GLOBAL.processing.stroke(64);
		GLOBAL.processing.noFill();
		GLOBAL.processing.ellipse(xy[0], xy[1], diameter, diameter);
		
	}
	
	
}
